---
title: Module API Reference
description: An API reference of Expo modules API.
sidebar_title: Module API
---

import { APIBox } from '~/components/plugins/APIBox';
import { CodeBlocksTable } from '~/components/plugins/CodeBlocksTable';
import { APIMethod } from '~/components/plugins/api/APIMethod';
import { FileTree } from '~/ui/components/FileTree';
import { PlatformTags, StatusTag } from '~/ui/components/Tag';
import { A, CALLOUT } from '~/ui/components/Text';

The native modules API is an abstraction layer on top of [JSI](https://reactnative.dev/architecture/glossary#javascript-interfaces-jsi) and other low-level primitives that React Native is built upon. It is built with modern languages (Swift and Kotlin) and provides an easy-to-use and convenient API that is consistent across platforms where possible.

## Definition components

As you might have noticed in the snippets on the [Get Started](/modules/get-started) page, each module class must implement the `definition` function.
The module definition consists of the DSL components that describe the module's functionality and behavior.

<APIBox header="Name">

Sets the name of the module that JavaScript code will use to refer to the module. Takes a string as an argument. This can be inferred from the module's class name, but it's recommended to set it explicitly for clarity.

```swift Swift / Kotlin
Name("MyModuleName")
```

</APIBox>
<APIBox header="Constants">

Sets constant properties on the module. Can take a dictionary or a closure that returns a dictionary.

<CodeBlocksTable>

```swift
// Created from the dictionary
Constants([
  "PI": Double.pi
])

// or returned by the closure
Constants {
  return [
    "PI": Double.pi
  ]
}
```

```kotlin
// Passed as arguments
Constants(
  "PI" to kotlin.math.PI
)

// or returned by the closure
Constants {
  return@Constants mapOf(
    "PI" to kotlin.math.PI
  )
}
```

</CodeBlocksTable>
</APIBox>
<APIBox header="Function">

Defines a native synchronous function that will be exported to JavaScript. Synchronous means that when the function is executed in JavaScript, its native code is run on the same thread and blocks further execution of the script until the native function returns.

#### Arguments

- **name**: `String` — Name of the function that you'll call from JavaScript.
- **body**: `(args...) -> ReturnType` — The closure to run when the function is called.

The function can receive up to 8 arguments. This is due to the limitations of generics in both Swift and Kotlin because this component must be implemented separately for each arity.

See the [Argument types](#argument-types) section for more details on what types can be used in the function body.

<CodeBlocksTable>

```swift
Function("syncFunction") { (message: String) in
  return message
}
```

```kotlin
Function("syncFunction") { message: String ->
  return@Function message
}
```

</CodeBlocksTable>

```js JavaScript
import { requireNativeModule } from 'expo-modules-core';

// Assume that we have named the module "MyModule"
const MyModule = requireNativeModule('MyModule');

function getMessage() {
  return MyModule.syncFunction('bar');
}
```

</APIBox>
<APIBox header="AsyncFunction">

Defines a JavaScript function that always returns a `Promise` and whose native code is by default dispatched on a different thread than the JavaScript runtime runs on.

#### Arguments

- **name**: `String` — Name of the function that you'll call from JavaScript.
- **body**: `(args...) -> ReturnType` — The closure to run when the function is called.

If the type of the last argument is `Promise`, the function will wait for the promise to be resolved or rejected before the response is passed back to JavaScript. Otherwise, the function is immediately resolved with the returned value or rejected if it throws an exception.
The function can receive up to 8 arguments (including the promise).

See the [Argument types](#argument-types) section for more details on what types can be used in the function body.

It is recommended to use `AsyncFunction` over `Function` when it:

- does I/O bound tasks such as sending network requests or interacting with the file system
- needs to be run on a different thread, for example, the main UI thread for UI-related tasks
- is an extensive or long-lasting operation that would block the JavaScript thread which in turn would reduce the responsiveness of the application

<CodeBlocksTable>

```swift
AsyncFunction("asyncFunction") { (message: String) in
  return message
}

// or

AsyncFunction("asyncFunction") { (message: String, promise: Promise) in
  promise.resolve(message)
}

```

```kotlin
AsyncFunction("asyncFunction") { message: String ->
  return@AsyncFunction message
}

// or

// Make sure to import `Promise` class from `expo.modules.kotlin` instead of `expo.modules.core`.
AsyncFunction("asyncFunction") { message: String, promise: Promise ->
  promise.resolve(message)
}

```

</CodeBlocksTable>

```js JavaScript
import { requireNativeModule } from 'expo-modules-core';

// Assume that we have named the module "MyModule"
const MyModule = requireNativeModule('MyModule');

async function getMessageAsync() {
  return await MyModule.asyncFunction('bar');
}
```

It is possible to change the native queue of `AsyncFunction` by calling the `.runOnQueue` function on the result of that component.

<CodeBlocksTable>

```swift
AsyncFunction("asyncFunction") { (message: String) in
  return message
}.runOnQueue(.main)
```

```kotlin
AsyncFunction("asyncFunction") { message: String ->
  return@AsyncFunction message
}.runOnQueue(Queues.MAIN)
```

</CodeBlocksTable>

---

#### Kotlin coroutines <PlatformTags prefix="" platforms={['android']} />

`AsyncFunction` can receive a suspendable body on Android. However, it has to be passed in the infix notation after the `Coroutine` block. You can read more about suspendable functions and coroutines on [coroutine overview](https://kotlinlang.org/docs/coroutines-overview.html).

`AsyncFunction` with a suspendable body can't receive `Promise` as an argument. It uses a suspension mechanism to execute asynchronous calls.
The function is immediately resolved with the returned value of the provided suspendable block or rejected if it throws an exception. The function can receive up to 8 arguments.

By default, suspend functions are dispatched on the module's coroutine scope. Moreover, every other suspendable function called from the body block is run within the same scope.
This scope's lifecycle is bound to the module's lifecycle - all unfinished suspend functions will be canceled when the module is deallocated.

```kotlin Kotlin
AsyncFunction("suspendFunction") Coroutine { message: String ->
  // You can execute other suspendable functions here.
  // For example, you can use `kotlinx.coroutines.delay` to delay resolving the underlying promise.
  delay(5000)
  return@Coroutine message
}
```

</APIBox>
<APIBox header="Events">

Defines event names that the module can send to JavaScript.

> **Note**: This component can be used inside of the [`View`](#view) block to define callback names. See [`View callbacks`](#view-callbacks)

<CodeBlocksTable>

```swift
Events("onCameraReady", "onPictureSaved", "onBarCodeScanned")
```

```kotlin
Events("onCameraReady", "onPictureSaved", "onBarCodeScanned")
```

</CodeBlocksTable>

See [Sending events](#sending-events) to learn how to send events from the native code to JavaScript/TypeScript.

</APIBox>
<APIBox header="Property">

Defines a new property directly on the JavaScript object that represents a native module. It is the same as calling [`Object.defineProperty`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty) on the module object.

To declare a read-only property, you can use a shorthanded syntax that requires two arguments:

- **name**: `String` — Name of the property that you'll use from JavaScript.
- **getter**: `() -> PropertyType` — The closure to run when the getter for a property was called.

<CodeBlocksTable>

```swift
Property("foo") {
  return "bar"
}
```

```kotlin
Property("foo") {
  return@Property "bar"
}
```

</CodeBlocksTable>

In the case of the mutable property, both the getter and the setter closure are needed (using the syntax below is also possible to declare a property with only a setter):

- **name**: `String` — Name of the property that you'll use from JavaScript.
- **getter**: `() -> PropertyType` — The closure to run when the getter for a property was called.
- **setter**: `(newValue: PropertyType) -> void` — The closure to run when the setter for a property was called.

<CodeBlocksTable>

```swift
Property("foo")
  .get { return "bar" }
  .set { (newValue: String) in
    // do something with new value
  }
```

```kotlin
Property("foo")
  .get { return@get "bar" }
  .set { newValue: String ->
    // do something with new value
  }
```

</CodeBlocksTable>

```js JavaScript
import { requireNativeModule } from 'expo-modules-core';

// Assume that we have named the module "MyModule"
const MyModule = requireNativeModule('MyModule');

// Obtain the property value
MyModule.foo;

// Set a new value
MyModule.foo = 'foobar';
```

</APIBox>
<APIBox header="View">

Enables the module to be used as a native view. Definition components that are accepted as part of the view definition: [`Prop`](#prop), [`Events`](#events), [`GroupView`](#groupview) and [`AsyncFunction`](#asyncfunction).

[`AsyncFunction`](#asyncfunction) in the view definition is added to the React ref of the React component representing the native view.
Such async functions automatically receive an instance of the native view as the first argument and run on the UI thread by default.

#### Arguments

- **viewType** — The class of the native view that will be rendered. Note: On Android, the provided class must inherit from the [`ExpoView`](#expoview), on iOS it's optional. See [`Extending ExpoView`](#extending--expoview).
- **definition**: `() -> ViewDefinition` — A builder of the view definition.

<CodeBlocksTable>

```swift
View(UITextView.self) {
  Prop("text") { ... }

  AsyncFunction("focus") { (view: UITextView) in
    view.becomeFirstResponder()
  }
}
```

```kotlin
View(TextView::class) {
  Prop("text") { ... }

  AsyncFunction("focus") { view: TextView ->
    view.requestFocus()
  }
}
```

</CodeBlocksTable>

> **Info** Support for rendering SwiftUI views is planned. For now, you can use [`UIHostingController`](https://developer.apple.com/documentation/swiftui/uihostingcontroller) and add its content view to your UIKit view.

> **Info** View functions are available as of SDK 49.

</APIBox>
<APIBox header="Prop">

Defines a setter for the view prop of given name.

#### Arguments

- **name**: `String` — Name of view prop that you want to define a setter.
- **setter**: `(view: ViewType, value: ValueType) -> ()` — Closure that is invoked when the view rerenders.

This property can only be used within a [`ViewManager`](#viewmanager) closure.

<CodeBlocksTable>

```swift
Prop("background") { (view: UIView, color: UIColor) in
  view.backgroundColor = color
}
```

```kotlin
Prop("background") { view: View, @ColorInt color: Int ->
  view.setBackgroundColor(color)
}
```

</CodeBlocksTable>

> **Note** Props of function type (callbacks) are not supported yet.

</APIBox>
<APIBox header="OnCreate">

Defines module's lifecycle listener that is called right after module initialization. If you need to set up something when the module gets initialized, use this instead of module's class initializer.

</APIBox>
<APIBox header="OnDestroy">

Defines module's lifecycle listener that is called when the module is about to be deallocated. Use it instead of module's class destructor.

</APIBox>
<APIBox header="OnStartObserving">

Defines the function that is invoked when the first event listener is added.

</APIBox>
<APIBox header="OnStopObserving">

Defines the function that is invoked when all event listeners are removed.

</APIBox>
<APIBox header="OnAppContextDestroys">

Defines module's lifecycle listener that is called when the app context owning the module is about to be deallocated.

</APIBox>
<APIBox header="OnAppEntersForeground" platforms={["ios"]}>

Defines the listener that is called when the app is about to enter the foreground mode.

> **Note** This function is not available on Android — you may want to use [`OnActivityEntersForeground`](#onactivityentersforeground) instead.

</APIBox>
<APIBox header="OnAppEntersBackground" platforms={["ios"]}>

Defines the listener that is called when the app enters the background mode.

> **Note** This function is not available on Android — you may want to use [`OnActivityEntersBackground`](#onactivityentersbackground) instead.

</APIBox>
<APIBox header="OnAppBecomesActive" platforms={["ios"]}>

Defines the listener that is called when the app becomes active again (after `OnAppEntersForeground`).

> **Note** This function is not available on Android — you may want to use [`OnActivityEntersForeground`](#onactivityentersforeground) instead.

</APIBox>
<APIBox header="OnActivityEntersForeground" platforms={["android"]}>

Defines the activity lifecycle listener that is called right after the activity is resumed.

> **Note** This function is not available on iOS — you may want to use [`OnAppEntersForeground`](#onappentersforeground) instead.

</APIBox>
<APIBox header="OnActivityEntersBackground" platforms={["android"]}>

Defines the activity lifecycle listener that is called right after the activity is paused.

> **Note** This function is not available on iOS — you may want to use [`OnAppEntersBackground`](#onappentersbackground) instead.

</APIBox>
<APIBox header="OnActivityDestroys" platforms={["android"]}>

Defines the activity lifecycle listener that is called when the activity owning the JavaScript context is about to be destroyed.

> **Note** This function is not available on iOS — you may want to use [`OnAppEntersBackground`](#onappentersbackground) instead.

</APIBox>

<APIBox header="GroupView" platforms={["android"]}>

Enables the view to be used as a view group. Definition components that are accepted as part of the group view definition: [`AddChildView`](#addchildview), [`GetChildCount`](#getchildcount), [`GetChildViewAt`](#getchildviewat), [`RemoveChildView`](#removechildview), [`RemoveChildViewAt`](#removechildviewat).

#### Arguments

- **viewType** — The class of the native view. Note that the provided class must inherit from the Android `ViewGroup`.
- **definition**: `() -> ViewGroupDefinition` — A builder of the view group definition.

This property can only be used within a [`View`](#view) closure.

```kotlin Kotlin
GroupView<ViewGroup> {
  AddChildView { parent, child, index -> ... }
}
```

</APIBox>

<APIBox header="AddChildView" platforms={["android"]}>

Defines action that adds a child view to the view group.

#### Arguments

- **action**: `(parent: ParentType, child: ChildType, index: Int) -> ()` — An action that adds a child view to the view group.

This property can only be used within a [`GroupView`](#groupview) closure.

```kotlin Kotlin
AddChildView { parent, child: View, index ->
  parent.addView(child, index)
}
```

</APIBox>

<APIBox header="GetChildCount" platforms={["android"]}>

Defines action the retrieves the number of child views in the view group.

#### Arguments

- **action**: `(parent: ParentType) -> Int` — A function that returns number of child views.

This property can only be used within a [`GroupView`](#groupview) closure.

```kotlin Kotlin
GetChildCount { parent ->
  return@GetChildCount parent.childCount
}
```

</APIBox>

<APIBox header="GetChildViewAt" platforms={["android"]}>

Defines action that retrieves a child view at a specific index from the view group.

#### Arguments

- **action**: `(parent: ParentType, index: Int) -> ChildType` — A function that retrieves a child view at a specific index from the view group.

This property can only be used within a [`GroupView`](#groupview) closure.

```kotlin Kotlin
GetChildViewAt { parent, index ->
  parent.getChildAt(index)
}
```

</APIBox>

<APIBox header="RemoveChildView" platforms={["android"]}>

Defines action that removes a specific child view from the view group.

#### Arguments

- **action**: `(parent: ParentType, child: ChildType) -> ()` — A function that remove a specific child view from the view group.

This property can only be used within a [`GroupView`](#groupview) closure.

```kotlin Kotlin
RemoveChildView { parent, child: View ->
  parent.removeView(child)
}
```

</APIBox>

<APIBox header="RemoveChildViewAt" platforms={["android"]}>

Defines action that removes a child view at a specific index from the view group.

#### Arguments

- **action**: `(parent: ParentType, child: ChildType) -> ()` — A function that removes a child view at a specific index from the view group.

This property can only be used within a [`GroupView`](#groupview) closure.

```kotlin Kotlin
RemoveChildViewAt { parent, index ->
  parent.removeViewAt(child)
}
```

</APIBox>

## Argument types

Fundamentally, only primitive and serializable data can be passed back and forth between the runtimes. However, usually native modules need to receive custom data structures — more sophisticated than just the dictionary/map where the values are of unknown (`Any`) type and so each value has to be validated and cast on its own. The Expo Modules API provides protocols to make it more convenient to work with data objects, to provide automatic validation, and finally, to ensure native type-safety on each object member.

<APIBox header="Primitives">

All functions and view prop setters accept all common primitive types in Swift and Kotlin as the arguments. This includes arrays, dictionaries/maps and optionals of these primitive types.

| Language | Supported primitive types                                                                                                      |
| -------- | ------------------------------------------------------------------------------------------------------------------------------ |
| Swift    | `Bool`, `Int`, `Int8`, `Int16`, `Int32`, `Int64`, `UInt`, `UInt8`, `UInt16`, `UInt32`, `UInt64`, `Float32`, `Double`, `String` |
| Kotlin   | `Boolean`, `Int`, `Long`, `Float`, `Double`, `String`, `Pair`                                                                  |

</APIBox>
<APIBox header="Convertibles">

_Convertibles_ are native types that can be initialized from certain specific kinds of data received from JavaScript. Such types are allowed to be used as an argument type in `Function`'s body. For example, when the `CGPoint` type is used as a function argument type, its instance can be created from an array of two numbers `(x, y)` or a JavaScript object with numeric `x` and `y` properties.

---

Some common iOS types from `CoreGraphics` and `UIKit` system frameworks are already made convertible.

| Native iOS Type         | TypeScript                                                                                                                                                                        |
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `URL`                   | `string` with a URL. When a scheme is not provided, it's assumed to be a file URL.                                                                                                |
| `CGFloat`               | `number`                                                                                                                                                                          |
| `CGPoint`               | `{ x: number, y: number }` or `number[]` with _x_ and _y_ coords                                                                                                                  |
| `CGSize`                | `{ width: number, height: number }` or `number[]` with _width_ and _height_                                                                                                       |
| `CGVector`              | `{ dx: number, dy: number }` or `number[]` with _dx_ and _dy_ vector differentials                                                                                                |
| `CGRect`                | `{ x: number, y: number, width: number, height: number }` or `number[]` with _x_, _y_, _width_ and _height_ values                                                                |
| `CGColor`<br/>`UIColor` | Color hex strings (`#RRGGBB`, `#RRGGBBAA`, `#RGB`, `#RGBA`), named colors following the [CSS3/SVG specification](https://www.w3.org/TR/css-color-3/#svg-color) or `"transparent"` |
| `Data`                  | `Uint8Array` <StatusTag note="SDK 50+" />                                                                                                                                         |

---

Similarly, some common Android types from packages like `java.io`, `java.net`, or `android.graphics` are also made convertible.

| Native Android Type                                                           | TypeScript                                                                                                                                                                        |
| ----------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `java.net.URL`                                                                | `string` with a URL. Note that the scheme has to be provided                                                                                                                      |
| `android.net.Uri`<br/>`java.net.URI`                                          | `string` with a URI. Note that the scheme has to be provided                                                                                                                      |
| `java.io.File`<br/>`java.nio.file.Path` (is only available on Android API 26) | `string` with a path to the file                                                                                                                                                  |
| `android.graphics.Color`                                                      | Color hex strings (`#RRGGBB`, `#RRGGBBAA`, `#RGB`, `#RGBA`), named colors following the [CSS3/SVG specification](https://www.w3.org/TR/css-color-3/#svg-color) or `"transparent"` |
| `kotlin.Pair<A, B>`                                                           | Array with two values, where the first one is of type _A_ and the second is of type _B_                                                                                           |
| `kotlin.ByteArray`                                                            | `Uint8Array` <StatusTag note="SDK 50+" />                                                                                                                                         |

</APIBox>
<APIBox header="Records">

_Record_ is a convertible type and an equivalent of the dictionary (Swift) or map (Kotlin), but represented as a struct where each field can have its type and provide a default value.
It is a better way to represent a JavaScript object with the native type safety.

<CodeBlocksTable>

```swift
struct FileReadOptions: Record {
  @Field
  var encoding: String = "utf8"

  @Field
  var position: Int = 0

  @Field
  var length: Int?
}

// Now this record can be used as an argument of the functions or the view prop setters.
Function("readFile") { (path: String, options: FileReadOptions) -> String in
  // Read the file using given `options`
}
```

```kotlin
class FileReadOptions : Record {
  @Field
  val encoding: String = "utf8"

  @Field
  val position: Int = 0

  @Field
  val length: Int? = null
}

// Now this record can be used as an argument of the functions or the view prop setters.
Function("readFile") { path: String, options: FileReadOptions ->
  // Read the file using given `options`
}
```

</CodeBlocksTable>
</APIBox>
<APIBox header="Enums">

With enums, we can go even further with the above example (with `FileReadOptions` record) and limit supported encodings to `"utf8"` and `"base64"`. To use an enum as an argument or record field, it must represent a primitive value (for example, `String`, `Int`) and conform to `Enumerable`.

<CodeBlocksTable>

```swift
enum FileEncoding: String, Enumerable {
  case utf8
  case base64
}

struct FileReadOptions: Record {
  @Field
  var encoding: FileEncoding = .utf8
  // ...
}
```

```kotlin
// Note: the constructor must have an argument called value.
enum class FileEncoding(val value: String) : Enumerable {
  utf8("utf8"),
  base64("base64")
}

class FileReadOptions : Record {
  @Field
  val encoding: FileEncoding = FileEncoding.utf8
  // ...
}
```

</CodeBlocksTable>
</APIBox>
<APIBox header="Eithers">

There are some use cases where you want to pass various types for a single function argument. This is where Either types might come in handy.
They act as a container for a value of one of a couple of types.

<CodeBlocksTable>

```swift
Function("foo") { (bar: Either<String, Int>) in
  if let bar: String = bar.get() {
    // `bar` is a String
  }
  if let bar: Int = bar.get() {
    // `bar` is an Int
  }
}
```

```kotlin
Function("foo") { bar: Either<String, Int> ->
  bar.get(String::class).let {
    // `it` is a String
  }
  bar.get(Int::class).let {
    // `it` is an Int
  }
}
```

</CodeBlocksTable>

The implementation for three Either types is currently provided out of the box, allowing you to use up to four different subtypes.

- `Either<FirstType, SecondType>` — A container for one of two types.
- `EitherOfThree<FirstType, SecondType, ThirdType>` — A container for one of three types.
- `EitherOfFour<FirstType, SecondType, ThirdType, FourthType>` — A container for one of four types.

</APIBox>
<APIBox header="JavaScript values">

It's also possible to use a `JavaScriptValue` type which is a holder for any value that can be represented in JavaScript.
This type is useful when you want to mutate the given argument or when you want to omit type validations and conversions.
Note that using JavaScript-specific types is restricted to synchronous functions as all reads and writes in the JavaScript runtime must happen on the JavaScript thread.
Any access to these values from different threads will result in a crash.

In addition to the raw value, the `JavaScriptObject` type can be used to allow only object types and `JavaScriptFunction<ReturnType>` for callbacks.

<CodeBlocksTable>

```swift
Function("mutateMe") { (value: JavaScriptValue) in
  if value.isObject() {
    let jsObject = value.getObject()
    jsObject.setProperty("expo", value: "modules")
  }
}

// or

Function("mutateMe") { (jsObject: JavaScriptObject) in
  jsObject.setProperty("expo", value: "modules")
}
```

```kotlin
Function("mutateMe") { value: JavaScriptValue ->
  if (value.isObject()) {
    val jsObject = value.getObject()
    jsObject.setProperty("expo", "modules")
  }
}

// or

Function("mutateMe") { jsObject: JavaScriptObject ->
  jsObject.setProperty("expo", "modules")
}
```

</CodeBlocksTable>

> **Info** JavaScript types are available as of SDK 49.

</APIBox>

## Native classes

<APIBox header="Module">

A base class for a native module.

#### Properties

<APIMethod
  name="appContext"
  comment="Provides access to the [`AppContext`](#appcontext)."
  returnTypeName="AppContext"
  isProperty
  isReturnTypeReference
/>

#### Methods

<APIMethod
  name="sendEvent"
  comment="Sends an event with a given name and a payload to JavaScript. See [`Sending events`](#sending-events)"
  returnTypeName="void"
  parameters={[
    {
      name: 'eventName',
      comment: 'The name of the JavaScript event',
      typeName: 'string',
    },
    {
      name: 'payload',
      comment: 'The event payload',
      typeName: 'Android: Map<String, Any?> | Bundle\niOS: [String: Any?]',
    },
  ]}
/>

</APIBox>

<APIBox header="AppContext">

The app context is an interface to a single Expo app.

#### Properties

<APIMethod
  name="constants"
  comment="Provides access to app's constants from legacy module registry."
  returnTypeName="Android: ConstantsInterface? iOS: EXConstantsInterface?"
  isProperty
/>

<APIMethod
  name="permissions"
  comment="Provides access to the permissions manager from legacy module registry."
  returnTypeName="Android: Permissions? iOS: EXPermissionsInterface?"
  isProperty
/>

<APIMethod
  name="imageLoader"
  comment="Provides access to the image loader from the legacy module registry."
  returnTypeName="Android: ImageLoaderInterface? iOS: EXImageLoaderInterface?"
  isProperty
/>

<APIMethod
  name="barcodeScanner"
  comment="Provides access to the bar code scanner manager from the legacy module registry."
  returnTypeName="ImageLoaderInterface?"
  isProperty
  platforms={['Android']}
/>

<APIMethod
  name="camera"
  comment="Provides access to the camera view manager from the legacy module registry."
  returnTypeName="CameraViewInterface?"
  isProperty
  platforms={['Android']}
/>

<APIMethod
  name="font"
  comment="Provides access to the font manager from the legacy module registry."
  returnTypeName="FontManagerInterface?"
  isProperty
  platforms={['Android']}
/>

<APIMethod
  name="sensor"
  comment="Provides access to the sensor manager from the legacy module registry."
  returnTypeName="SensorServiceInterface?"
  isProperty
  platforms={['Android']}
/>

<APIMethod
  name="taskManager"
  comment="Provides access to the task manager from the legacy module registry."
  returnTypeName="TaskManagerInterface?"
  isProperty
  platforms={['Android']}
/>

<APIMethod
  name="activityProvider"
  comment="Provides access to the activity provider from the legacy module registry."
  returnTypeName="ActivityProvider?"
  isProperty
  platforms={['Android']}
/>

<APIMethod
  name="reactContext"
  comment="Provides access to the react application context."
  returnTypeName="Context?"
  isProperty
  platforms={['Android']}
/>

<APIMethod
  name="hasActiveReactInstance"
  comment="Checks if there is an not-null, alive react native instance."
  returnTypeName="Boolean"
  isProperty
  platforms={['Android']}
/>

<APIMethod
  name="utilities"
  comment="Provides access to the utilities from legacy module registry."
  returnTypeName="EXUtilitiesInterface?"
  isProperty
  platforms={['iOS']}
/>

</APIBox>

<APIBox header="ExpoView">

A base class that should be used by all exported views.

On iOS, `ExpoView` extends the `RCTView` which handles some styles (for example, borders) and accessibility.

#### Properties

<APIMethod
  name="appContext"
  comment="Provides access to the [`AppContext`](#appcontext)."
  returnTypeName="AppContext"
  isProperty
  isReturnTypeReference
/>

#### Extending `ExpoView`

To export your view using the [`View`](#view) component, your custom class must inherit from the `ExpoView`. By doing that you will get access to the [`AppContext`](#appcontext) object. It's the only way of communicating with other modules and the JavaScript runtime. Also, you can't change constructor parameters, because provided view will be initialized by `expo-modules-core`.

<CodeBlocksTable>

```swift
class LinearGradientView: ExpoView {}

public class LinearGradientModule: Module {
  public func definition() -> ModuleDefinition {
    View(LinearGradientView.self) {
      // ...
    }
  }
}
```

```kotlin
class LinearGradientView(
  context: Context,
  appContext: AppContext,
) : ExpoView(context, appContext)

class LinearGradientModule : Module() {
  override fun definition() = ModuleDefinition {
    View(LinearGradientView::class) {
      // ...
    }
  }
}
```

</CodeBlocksTable>

</APIBox>

## Guides

<APIBox>
### Sending events

While JavaScript/TypeScript to Native communication is mostly covered by native functions, you might also want to let the JavaScript/TypeScript code know about certain system events, for example, when the clipboard content changes.

To do this, in the module definition, you need to provide the event names that the module can send using the [Events](#events) definition component. After that, you can use the `sendEvent(eventName, payload)` function on the module instance to send the actual event with some payload. For example, a minimal clipboard implementation that sends native events may look like this:

<CodeBlocksTable>

```swift
let CLIPBOARD_CHANGED_EVENT_NAME = "onClipboardChanged"

public class ClipboardModule: Module {
  public func definition() -> ModuleDefinition {
    Events(CLIPBOARD_CHANGED_EVENT_NAME)

    OnStartObserving {
      NotificationCenter.default.addObserver(
        self,
        selector: #selector(self.clipboardChangedListener),
        name: UIPasteboard.changedNotification,
        object: nil
      )
    }

    OnStopObserving {
      NotificationCenter.default.removeObserver(
        self,
        name: UIPasteboard.changedNotification,
        object: nil
      )
    }
  }

  @objc
  private func clipboardChangedListener() {
    sendEvent(CLIPBOARD_CHANGED_EVENT_NAME, [
      "contentTypes": availableContentTypes()
    ])
  }
}
```

```kotlin
const val CLIPBOARD_CHANGED_EVENT_NAME = "onClipboardChanged"

class ClipboardModule : Module() {
  override fun definition() = ModuleDefinition {
    Events(CLIPBOARD_CHANGED_EVENT_NAME)

    OnStartObserving {
      clipboardManager?.addPrimaryClipChangedListener(listener)
    }

    OnStopObserving {
      clipboardManager?.removePrimaryClipChangedListener(listener)
    }
  }

  private val clipboardManager: ClipboardManager?
    get() = appContext.reactContext?.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager

  private val listener = ClipboardManager.OnPrimaryClipChangedListener {
    clipboardManager?.primaryClipDescription?.let { clip ->
      this@ClipboardModule.sendEvent(
        CLIPBOARD_CHANGED_EVENT_NAME,
        bundleOf(
          "contentTypes" to availableContentTypes(clip)
        )
      )
    }
  }
}
```

</CodeBlocksTable>

To subscribe to these events in JavaScript/TypeScript, you need to wrap the native module with `EventEmitter` class as shown:

```ts TypeScript
import { requireNativeModule, EventEmitter, Subscription } from 'expo-modules-core';

const ClipboardModule = requireNativeModule('Clipboard');
const emitter = new EventEmitter(ClipboardModule);

export function addClipboardListener(listener: (event) => void): Subscription {
  return emitter.addListener('onClipboardChanged', listener);
}
```

</APIBox>

<APIBox>
### View callbacks

Some events are connected to a certain view. For example, the touch event should be sent only to the underlying JavaScript view which was pressed. In that case, you can't use `sendEvent` described in [`Sending events`](#sending-events). The `expo-modules-core` introduces a view callbacks mechanism to handle view-bound events.

To use it, in the view definition, you need to provide the event names that the view can send using the [Events](#events) definition component. After that, you need to declare a property of type `EventDispatcher` in your view class. The name of the declared property has to be the same as the name exported in the `Events` component. Later, you can call it as a function and pass a payload of type `[String: Any?]` on iOS and `Map<String, Any?>` on Android.

> **Note**: On Android, it's possible to specify the payload type. In case of types that don't convert into objects, the payload will be encapsulated and stored under the `payload` key: `{payload: <provided value>}`.

<CodeBlocksTable>

```swift
class CameraViewModule: Module {
  public func definition() -> ModuleDefinition {
    View(CameraView.self) {
      Events(
        "onCameraReady"
      )

      // ...
    }
  }
}

class CameraView: ExpoView {
  let onCameraReady = EventDispatcher()

  func callOnCameraReady() {
    onCameraReady([
      "message": "Camera was mounted"
    ]);
  }
}
```

```kotlin
class CameraViewModule : Module() {
  override fun definition() = ModuleDefinition {
    View(ExpoCameraView::class) {
      Events(
        "onCameraReady"
      )

      // ...
    }
  }
}

class CameraView(
  context: Context,
  appContext: AppContext
) : ExpoView(context, appContext) {
  val onCameraReady by EventDispatcher()

  fun callOnCameraReady() {
    onCameraReady(mapOf(
      "message" to "Camera was mounted"
    ));
  }
}
```

</CodeBlocksTable>

To subscribe to these events in JavaScript/TypeScript, you need to pass a function to the native view as shown:

```ts TypeScript
import { requireNativeViewManager } from 'expo-modules-core';

const CameraView = requireNativeViewManager('CameraView');

export default function MainView() {
  const onCameraReady = event => {
    console.log(event.nativeEvent);
  };

  return <CameraView onCameraReady={onCameraReady} />;
}
```

Provided payload is available under the `nativeEvent` key.

</APIBox>

## Examples

<CodeBlocksTable>

```swift
public class MyModule: Module {
  public func definition() -> ModuleDefinition {
    Name("MyFirstExpoModule")

    Function("hello") { (name: String) in
      return "Hello \(name)!"
    }
  }
}
```

```kotlin
class MyModule : Module() {
  override fun definition() = ModuleDefinition {
    Name("MyFirstExpoModule")

    Function("hello") { name: String ->
      return "Hello $name!"
    }
  }
}
```

</CodeBlocksTable>

For more examples from real modules, you can refer to Expo modules that already use this API on GitHub:

<FileTree
  files={[
    [
      'expo-battery',
      <A href="https://github.com/expo/expo/tree/main/packages/expo-battery/ios">Swift</A>,
    ],
    [
      'expo-cellular',
      <CALLOUT>
        <A href="https://github.com/expo/expo/tree/main/packages/expo-cellular/android/src/main/java/expo/modules/cellular">
          Kotlin
        </A>
        , <A href="https://github.com/expo/expo/tree/main/packages/expo-cellular/ios">Swift</A>
      </CALLOUT>,
    ],
    [
      'expo-clipboard',
      <CALLOUT>
        <A href="https://github.com/expo/expo/tree/main/packages/expo-clipboard/android/src/main/java/expo/modules/clipboard">
          Kotlin
        </A>
        , <A href="https://github.com/expo/expo/tree/main/packages/expo-clipboard/ios">Swift</A>
      </CALLOUT>,
    ],
    [
      'expo-crypto',
      <CALLOUT>
        <A href="https://github.com/expo/expo/tree/main/packages/expo-crypto/android/src/main/java/expo/modules/crypto">
          Kotlin
        </A>
        , <A href="https://github.com/expo/expo/tree/main/packages/expo-crypto/ios">Swift</A>
      </CALLOUT>,
    ],
    [
      'expo-device',
      <A href="https://github.com/expo/expo/tree/main/packages/expo-device/ios">Swift</A>,
    ],
    [
      'expo-haptics',
      <A href="https://github.com/expo/expo/tree/main/packages/expo-haptics/ios">Swift</A>,
    ],
    [
      'expo-image-manipulator',
      <A href="https://github.com/expo/expo/tree/main/packages/expo-image-manipulator/ios">
        Swift
      </A>,
    ],
    [
      'expo-image-picker',
      <CALLOUT>
        <A href="https://github.com/expo/expo/tree/main/packages/expo-image-picker/android/src/main/java/expo/modules/imagepicker">
          Kotlin
        </A>
        , <A href="https://github.com/expo/expo/tree/main/packages/expo-image-picker/ios">Swift</A>
      </CALLOUT>,
    ],
    [
      'expo-linear-gradient',
      <CALLOUT>
        <A href="https://github.com/expo/expo/tree/main/packages/expo-linear-gradient/android/src/main/java/expo/modules/lineargradient">
          Kotlin
        </A>
        ,{' '}
        <A href="https://github.com/expo/expo/tree/main/packages/expo-linear-gradient/ios">Swift</A>
      </CALLOUT>,
    ],
    [
      'expo-localization',
      <CALLOUT>
        <A href="https://github.com/expo/expo/tree/main/packages/expo-localization/android/src/main/java/expo/modules/localization">
          Kotlin
        </A>
        , <A href="https://github.com/expo/expo/tree/main/packages/expo-localization/ios">Swift</A>
      </CALLOUT>,
    ],
    [
      'expo-store-review',
      <A href="https://github.com/expo/expo/tree/main/packages/expo-store-review/ios">Swift</A>,
    ],
    [
      'expo-system-ui',
      <A href="https://github.com/expo/expo/tree/main/packages/expo-system-ui/ios/ExpoSystemUI">
        Swift
      </A>,
    ],
    [
      'expo-video-thumbnails',
      <A href="https://github.com/expo/expo/tree/main/packages/expo-video-thumbnails/ios">Swift</A>,
    ],
    [
      'expo-web-browser',
      <CALLOUT>
        <A href="https://github.com/expo/expo/tree/main/packages/expo-web-browser/android/src/main/java/expo/modules/webbrowser">
          Kotlin
        </A>
        , <A href="https://github.com/expo/expo/tree/main/packages/expo-web-browser/ios">Swift</A>
      </CALLOUT>,
    ],
  ]}
/>
